CLASS zcl_dome_api_001 DEFINITION
  PUBLIC
  CREATE PUBLIC .

  PUBLIC SECTION.

    INTERFACES if_http_service_extension .

    INTERFACES if_oo_adt_classrun .

    METHODS demo001
      IMPORTING
        VALUE(io_request) TYPE REF TO if_web_http_request OPTIONAL
      EXPORTING
        " returning
        VALUE(ev_json)    TYPE string .

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS ZCL_DOME_API_001 IMPLEMENTATION.


  METHOD demo001.
    DATA(lv_text) = io_request->get_text( )." 获取JSON
    DATA:BEGIN OF out_data,
           mesg      TYPE string,
           werks(4)  TYPE c,
           matnr(40) TYPE c,
         END OF out_data.

    DATA:BEGIN OF is_data,
           purchaseorder(10) TYPE c,

         END OF  is_data.

    CALL METHOD /ui2/cl_json=>deserialize
      EXPORTING
        json = lv_text
      CHANGING
        data = is_data.


    IF is_data IS NOT INITIAL and is_data-purchaseorder = '4100000003'.
      out_data-mesg = '查询成功'.
      out_data-werks = '1001'.
      out_data-matnr = 'R0412'.
    ELSE.
      out_data-mesg = '查询失败'.
      out_data-werks = ''.
      out_data-matnr = ''.
    ENDIF.
    "ev_json = 'METHOD demo001.'.
    ev_json = /ui2/cl_json=>serialize( data = out_data ).

  ENDMETHOD.


  METHOD if_http_service_extension~handle_request.

    DATA lv_ev_json    TYPE string .
    " DATA(lv_text) = request->get_text( )." 获取JSON
    DATA(lv_action) = request->get_form_field( 'action' ).


    CASE request->get_method( ).

      WHEN CONV string( if_web_http_client=>post ).


        " response->set_text('访问成功POST！' && lv_action ).

        " POSTMAN001
        " TZZEaQ@c<kH}E~&]P6AnXnLq6R<$gaKyWY3QBW#5


        "调用对应的方法
        CASE lv_action.
          WHEN 'demo001' .
            TRY.
                CALL METHOD me->demo001
                  EXPORTING
                    io_request = request
                  IMPORTING
                    ev_json    = lv_ev_json.
                response->set_text(  lv_ev_json  ).

              CATCH cx_sy_dyn_call_error INTO DATA(lo_exc).
                response->set_text( 'Connection error:' && lo_exc->get_text( ) ).
            ENDTRY.
          WHEN ''.
        ENDCASE.

*        "调用对应的方法  --无法使用
*        TRY.
*            CALL METHOD me->(lv_action)
*              EXPORTING
*                io_request = request
*              IMPORTING
*                ev_json    = lv_ev_json.
*            response->set_text(  lv_ev_json  ).
*
*          CATCH cx_sy_dyn_call_error INTO DATA(lo_exc).
*            response->set_text( 'Connection error:' && lo_exc->get_text( ) ).
*        ENDTRY.

      WHEN CONV string( if_web_http_client=>get ).

        response->set_text('访问成功get！' && lv_action ).


    ENDCASE.
  ENDMETHOD.
ENDCLASS.
